package com.authine.cloudpivot.ext.controller;

import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.view.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.Transformers;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 定时任务
 */
@Slf4j
@RestController
@RequestMapping("/api/scheduledTask")
public class ScheduledTaskController extends BaseController{

    @PersistenceContext
    EntityManager entityManager;

    /**
     * 活动日期结束后修改状态为已结束
     */
    @Scheduled(cron = "0 0 8 * * ?")
    @Transactional
    public void updatehdglstatus(){
        log.info("开始修改状态");
        StringBuilder sql=new StringBuilder();
        sql.append("SELECT * FROM `iwrwy_sbhdNew`  where toTime<=DATE(NOW())");
        List<Map<String,Object>> resultList = entityManager.createNativeQuery(sql.toString()).unwrap(NativeQueryImpl.class)
                .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).getResultList();
        if (CollectionUtils.isNotEmpty(resultList)){
            log.info("需要修改的数据:{}",resultList);
            List<String> id = resultList.stream().map(m -> (String) m.get("id")).collect(Collectors.toList());
            for (String s: id) {
                StringBuilder updatesql=new StringBuilder("update   iwrwy_sbhdNew set activeState='已结束' where id ='");
                updatesql.append(s).append("'");
                entityManager.createNativeQuery(updatesql.toString()).executeUpdate();
            }
        }
    }

    /**
     * 更新课程更新反馈状态
     */
    @Scheduled(cron = "0 0 8 * * ?")
    @Transactional
    public void updatekcgxglStatus(){
        log.info("开始修改状态");
        StringBuilder sql=new StringBuilder();
        sql.append("select * from iwrwy_courseUpdate where toTime<=Date(NOW())");
        List<Map<String,Object>> resultList = entityManager.createNativeQuery(sql.toString()).unwrap(NativeQueryImpl.class)
                .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).getResultList();
        if (CollectionUtils.isNotEmpty(resultList)){
            log.info("需要修改的数据:{}",resultList);
            List<String> id = resultList.stream().map(m -> (String) m.get("id")).collect(Collectors.toList());
            for (String s: id) {
                StringBuilder updatesql=new StringBuilder("update   iwrwy_courseUpdate set dataStatus='已结束' where id ='");
                updatesql.append(s).append("'");
                entityManager.createNativeQuery(updatesql.toString()).executeUpdate();
            }
        }
    }

    /**
     * 更新评聘活动状态
     */
    @Scheduled(cron = "0 0 8 * * ?")
    @Transactional
    public void updatepphdStatus(){
        log.info("开始修改状态");
        StringBuilder sql=new StringBuilder();
        sql.append("select * from iwrwy_pphd where sbjsrq<=Date(NOW())");
        List<Map<String,Object>> resultList = entityManager.createNativeQuery(sql.toString()).unwrap(NativeQueryImpl.class)
                .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).getResultList();
        if (CollectionUtils.isNotEmpty(resultList)){
            log.info("需要修改的数据:{}",resultList);
            List<String> id = resultList.stream().map(m -> (String) m.get("id")).collect(Collectors.toList());
            for (String s: id) {
                StringBuilder updatesql=new StringBuilder("update   iwrwy_pphd set hdzt='已结束' where id ='");
                updatesql.append(s).append("'");
                entityManager.createNativeQuery(updatesql.toString()).executeUpdate();
            }
        }
    }
}
